//
//  LXBookingCarInfoView.m
//  lixing
//
//  Created by ypj on 2022/6/25.
//

#import "LXBookingCarInfoView.h"
#import "LXAppointmentModel.h"
@interface LXBookingCarInfoView()
@property (nonatomic, strong) UIView *carInfoView,*carTipsView;
@property (nonatomic, strong) UIImageView *advertisingImg;
@end

@implementation LXBookingCarInfoView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
      
        self.backgroundColor = UIColor.clearColor;
 
    }
    return self;
}

- (void)setLB:(UILabel *)label text:(NSString *)text textColor:(UIColor *)textColor font:(CGFloat)font
{
    label.text = text;
    label.textColor = textColor;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = NSTextAlignmentCenter;
}

-(void)calllPhone:(UIButton *)btn{
    
    if (btn.tag == 0) {
//        [self callPhoneWithStr:self.model.userPhone];
    }
    else {
        [self callPhoneWithStr:self.model.businessPhone];
    }
}

- (void)setModel:(LXAppointmentModel *)model{
    _model = model;

    UIView *carInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, !StringIsEmpty(model.carTypeId)?104:140)];
    carInfoView.backgroundColor = UIColor.whiteColor;
    [carInfoView addCorner:5];
    self.carInfoView = carInfoView;
    [self addSubview:carInfoView];
    
    // 品牌型号
    UILabel *carBrandLB = [[UILabel alloc] initWithFrame:CGRectMake(15, 12, self.width - 20, 25)];
    carBrandLB.font = [UIFont boldSystemFontOfSize:18];
    carBrandLB.textColor = ColorWith333333;
    carBrandLB.text = [NSString stringWithFormat:@"%@ %@",[NSString stringWithNull:model.carBrand], [NSString stringWithNull:model.carMode]];
    [carInfoView addSubview:carBrandLB];
    
    
    NSMutableArray *carAry = @[
        @{
            @"key":@"能源类型",
            @"value":[Tool getId:model.energeType DicDataWithKey:k_NYLX]
        },
        @{
            @"key":@"运行平台",
            @"value":[Tool getId:model.runPlatfom DicDataWithKey:k_YXPT]
        },
        @{
            @"key":@"车辆类型",
            @"value":[Tool getId:model.carType DicDataWithKey:k_CLLX]
        }


    ].mutableCopy;
    // 非油车需要展示续航
    if ([model.energeType intValue] != 1) {
        [carAry insertObject:@{
            @"key":@"车辆续航",
            @"value":StringIsEmpty(model.endurance)?@"-":[NSString stringWithFormat:@"%@公里",[NSString stringWithNull:model.endurance]]
        } atIndex:1];
    }

    CGFloat width = (self.carInfoView.width) / carAry.count;
    for (int i = 0; i < carAry.count; i++) {

        NSDictionary *dic = carAry[i];

        UILabel *typeLB = [[UILabel alloc] initWithFrame:CGRectMake((i * width), carBrandLB.Y_height + 18, width, 14)];
        [self setLB:typeLB text:dic[@"key"] textColor:ColorWithAAAAAA font:12];
        [carInfoView addSubview:typeLB];
        UILabel *carTypeLB = [[UILabel alloc] initWithFrame:CGRectMake((i * width), typeLB.Y_height + 4, width, 14)];
        [self setLB:carTypeLB text:dic[@"value"] textColor:ColorWith333333 font:14];
        [carInfoView addSubview:carTypeLB];
    }
    
    
    if (StringIsEmpty(model.carTypeId)) {
        UIView *carTipsView = [[UIView alloc] initWithFrame:CGRectMake(15, carInfoView.height - 40, self.width - 30, 30)];
        [carTipsView setClipsToBounds:YES];
        carTipsView.backgroundColor = [[UIColor colorWithHexString:@"#EA545A"] colorWithAlphaComponent:0.3];
        [carTipsView addCorner:2];
        [carInfoView addSubview:carTipsView];
        
        
        
        UIButton *tipsBtn1 = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 200, carTipsView.height)];
        [tipsBtn1 setTitleColor:[UIColor colorWithHexString:@"#EA545A"] forState:UIControlStateNormal];
        [tipsBtn1 setTitle:@" 当前车库无此车型" forState:UIControlStateNormal];
        [tipsBtn1 setImage:Image(@"red_tips") forState:UIControlStateNormal];
        tipsBtn1.titleLabel.font = [UIFont systemFontOfSize:12];
        tipsBtn1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [carTipsView addSubview:tipsBtn1];
        
        UIButton *tipsBtn2 = [[UIButton alloc] initWithFrame:CGRectMake(carTipsView.width - 90, 0, 85, carTipsView.height)];
        self.tipsBtn2 = tipsBtn2;
        [tipsBtn2 setTitle:@"点击发布" forState:UIControlStateNormal];
        [tipsBtn2 setImage:Image(@"red_rightAcc") forState:UIControlStateNormal];
        [tipsBtn2 setTitleColor:[UIColor colorWithHexString:@"#EA545A"] forState:UIControlStateNormal];
        tipsBtn2.titleLabel.font = [UIFont systemFontOfSize:12];
        tipsBtn2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [tipsBtn2 setIconInRightWithSpacing:3];
        
        [carTipsView addSubview:tipsBtn2];
    }
    
    //最低价
    UIView *floorPriceView = [[UIView alloc] initWithFrame:CGRectMake(0, carInfoView.Y_height + 10, self.width, 50)];
    floorPriceView.backgroundColor = UIColor.whiteColor;
    [floorPriceView addCorner:3];
    [self addSubview:floorPriceView];
    
    UILabel *startAmountLB = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 50, floorPriceView.height)];
    startAmountLB.text = @"最低价";
    startAmountLB.textColor = ColorWith333333;
    startAmountLB.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    [floorPriceView addSubview:startAmountLB];
    
    //金额 ***起
    UILabel *amountLB = [[UILabel alloc] initWithFrame:CGRectMake(startAmountLB.X_width, 0, self.width - startAmountLB.X_width - 10, startAmountLB.height)];
    amountLB.textColor = [UIColor colorWithHexString:@"#EA545A"];
    amountLB.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
    amountLB.textAlignment = NSTextAlignmentRight;
    amountLB.text = [NSString stringWithFormat:@"%@ 元/月起",model.floorPrice];
    [amountLB addAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]} forString:@"元/月起" size:12];
    [floorPriceView addSubview:amountLB];
    
   
    //注意
    UILabel *attentionLB = [[UILabel alloc] initWithFrame:CGRectMake(15, floorPriceView.Y_height + 10, ScreenWidth, 30)];
    attentionLB.text = @"*最终签约价格以实际合同签约价格为准，当前报价仅供参考";
    attentionLB.textColor = ColorWithEA545A;
    attentionLB.numberOfLines = 2;
    attentionLB.font = [UIFont systemFontOfSize:12];
    attentionLB.textAlignment = NSTextAlignmentLeft;
    [self addSubview:attentionLB];
    
    UIView *contactView = [[UIView alloc] initWithFrame:CGRectMake(0, attentionLB.Y_height + 10, self.width, 140)];
    [contactView addCorner:4];
    contactView.backgroundColor = UIColor.whiteColor;
    [self addSubview:contactView];
    
    UILabel *contactLB = [[UILabel alloc] initWithFrame:CGRectMake(startAmountLB.X, 12, 150, 22)];
    contactLB.text = @"联系方式";
    contactLB.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    contactLB.textColor = ColorWith333333;
    [contactView addSubview:contactLB];
    
    NSString *uName = @"-";
    if (!StringIsEmpty(model.dname)) {
        uName = model.dname;
    }
    else if (!StringIsEmpty(model.remarkName)) {
        uName = model.remarkName;
    }
    else if (!StringIsEmpty(model.nickname)) {
        uName = model.nickname;
    }
    
    NSMutableArray *personary = @[
        @{
            @"title":@"客户信息",
            @"value":uName
        },
        @{
            @"title":@"利行联系人",
            @"value":[NSString stringWithFormat:@"%@ %@",[NSString stringWithNull:model.businessName],[NSString stringWithNull:model.businessPhone]]
        },
    ].mutableCopy;
    
   
    
    CGFloat contactY = contactLB.Y_height + 10;
    
    for (int i = 0; i < personary.count; i++) {
        
        NSDictionary *dic = personary[i];
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(15, contactY, contactView.width, 50)];
        [view setClipsToBounds:YES];
        [contactView addSubview:view];
        
        UILabel *nameL = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, view.height)];
        nameL.text = dic[@"title"];
        nameL.textColor = ColorWithAAAAAA;
        nameL.numberOfLines = 2;
        nameL.font = [UIFont systemFontOfSize:14];
        nameL.textAlignment = NSTextAlignmentLeft;
        [view addSubview:nameL];
        
        
        UILabel *valueL = [[UILabel alloc] initWithFrame:CGRectMake(nameL.X_width, nameL.Y, 200, view.height)];
        valueL.text = dic[@"value"];
        valueL.textColor = ColorWith333333;
        valueL.font = [UIFont systemFontOfSize:14];
        valueL.textAlignment = NSTextAlignmentLeft;
        [view addSubview:valueL];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, view.height - 1, view.width, 1)];
        line.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        [view addSubview:line];
        
        if(i != 0){
            UIButton *phoneBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
            phoneBtn.frame = CGRectMake(valueL.X_width, 0, view.width - valueL.X_width, view.height);
            phoneBtn.titleLabel.font = [UIFont systemFontOfSize:12];
            [phoneBtn setImage:[UIImage imageNamed:@"phone-fill"] forState:UIControlStateNormal];
            phoneBtn.tag =  i;
            [phoneBtn addTarget:self action:@selector(calllPhone:) forControlEvents:UIControlEventTouchUpInside];
            [phoneBtn setTitleColor:ColorWith3C6CFC forState:(UIControlStateNormal)];
            [view addSubview:phoneBtn];
           
        }
        contactY = view.Y_height;
        
    }
    
   





    UIImage *image = [UIImage imageNamed:@"icon_security"];
    self.advertisingImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, contactView.Y_height + 10, self.width, image.size.height)];
    self.advertisingImg.image = image;
    [self addSubview:self.advertisingImg];
    
    
    
    
}
-(void)callPhoneWithStr:(NSString *)phone{
    if (StringIsEmpty(phone))return;
    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"telprompt://%@",phone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:nil];
}
@end
